package com.highspeed.bdk;

import com.bianmaba.spring.mvc.method.support.annotations.JSON;
import com.highspeed.bdk.service.security.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by chenjiabin on 2018/3/23.
 */
@RestController
public class GlobalController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "get_current_user")
    @JSON
    public Object getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            Object obj = authentication.getPrincipal();
            return obj;
        } else {
            return null;
        }
    }


    @RequestMapping(value = "get_localhost")
    @JSON
    public Object getLocalhost(HttpServletRequest request) {
        String host = request.getRemoteHost();
        return host;
    }


    @RequestMapping(value = "load_initial_information")
    @JSON()
    public Object loadInitialInformation(HttpServletRequest request) {
        Map<String, Object> inf = new LinkedHashMap<String, Object>(0);
        Object user = getCurrentUser();
        String host = request.getRemoteHost();
        inf.put("user", user);
        inf.put("localhost", host);
        return inf;
    }
}
